﻿// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_PROD_MRP_QTY
// * CreateDate    : 2011-11-21 16:55:21
// * Creater       : CodeSmith
// * LastChangeDate: 2011-11-21 16:55:21
// * LastChanger   : 2011-11-21 16:55:21
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;
using ESOL.Common;

namespace ESOL.Business.Prod
{
    /// <summary>
    /// ProdMrpQty
    /// </summary>
    public partial class ProdMrpQtyBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objProdMrpQtyEntity">Entity</param>
        /// <returns>Entity</returns>
        public static ProdMrpQtyEntity GetProdMrpQty(ProdMrpQtyEntity objProdMrpQtyEntity)
        {
            return DataAccess.SelectSingle<ProdMrpQtyEntity>(objProdMrpQtyEntity);
        }
        public static ProdMrpQtyEntity GetByProductId(string productId)
        {
            if (string.IsNullOrEmpty(productId))
            {
                return null;
            }
            ProdMrpQtyEntity entity = new ProdMrpQtyEntity();
            entity.ProductId = productId;
            return DataAccess.SelectSingle<ProdMrpQtyEntity>(entity);
        }
        #endregion

        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objProdMrpQtyEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertProdMrpQty(ProdMrpQtyEntity objProdMrpQtyEntity)
        {
            return DataAccess.Insert(objProdMrpQtyEntity);
        }

        public static bool InsertProdMrpQty(List<ProdMrpQtyEntity> objProdMrpQtyEntityList)
        {
            return DataAccess.Insert(objProdMrpQtyEntityList);
        }
        #endregion

        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objProdMrpQtyEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateProdMrpQty(ProdMrpQtyEntity objProdMrpQtyEntity)
        {
            return DataAccess.Update(objProdMrpQtyEntity);
        }

        public static bool UpdateProdMrpQty(List<ProdMrpQtyEntity> objProdMrpQtyEntityList)
        {
            return DataAccess.Update<ProdMrpQtyEntity>(objProdMrpQtyEntityList);
        }
        #endregion

        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objProdMrpQtyEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteProdMrpQty(ProdMrpQtyEntity objProdMrpQtyEntity)
        {
            return DataAccess.Delete(objProdMrpQtyEntity);
        }

        public static bool DeleteProdMrpQty(List<ProdMrpQtyEntity> objProdMrpQtyEntityList)
        {
            return DataAccess.Delete<ProdMrpQtyEntity>(objProdMrpQtyEntityList);
        }
        #endregion

        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetProdMrpQtyAll()
        {
            return DataAccess.SelectAllDataSet<ProdMrpQtyEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<ProdMrpQtyEntity> GetProdMrpQtyAllList()
        {
            return DataAccess.SelectAll<ProdMrpQtyEntity>();
        }
        #endregion

        #region Functions
        public static bool UpdateAvailableQty()
        {
            string Sql = @"SELECT A.*,
                (CASE WHEN INVS.CURRENT_QTY IS NULL THEN 0 ELSE INVS.CURRENT_QTY END) AS CURRENT_QTY,
                (CASE WHEN B.PURCHASE_TOTAL_QTY IS NULL THEN 0 ELSE B.PURCHASE_TOTAL_QTY END) AS PURCHASE_TOTAL_QTY 
                FROM (SELECT PPOI.PRODUCT_ID,SUM(PPOI.QTY) AS PLAN_TOTAL_QTY
                FROM PROD_PLAN_ORDER_ITEM PPOI
                WHERE PPOI.STATUS IN ('POI01','POI03')
                GROUP BY PPOI.PRODUCT_ID
                ) AS A LEFT JOIN INV_STORAGE INVS ON A.PRODUCT_ID=INVS.PRODUCT_ID
                LEFT JOIN 
                (
                SELECT SUM(QTY) AS PURCHASE_TOTAL_QTY,PRODUCT_ID FROM PUR_PURCHASE_ORDER_ITEM
                GROUP BY PRODUCT_ID
                ) AS B ON A.PRODUCT_ID=B.PRODUCT_ID ";
            DataSet ds = DataAccess.SelectDataSet(Sql);
            DataSet ds2 = GetProdMrpQtyAll();

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    //broker.ExecuteNonQuery("", null, CommandType.StoredProcedure);
                    if (ds != null && ds.Tables[0] != null && ds2 != null && ds2.Tables[0] != null)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            bool flag = false;
                            decimal CURRENT_QTY = Convert.ToDecimal(dr["CURRENT_QTY"].ToString());
                            decimal PURCHASE_TOTAL_QTY = Convert.ToDecimal(dr["PURCHASE_TOTAL_QTY"].ToString());
                            decimal PLAN_TOTAL_QTY = Convert.ToDecimal(dr["PLAN_TOTAL_QTY"].ToString());
                            foreach (DataRow dr2 in ds2.Tables[0].Rows)
                            {
                                if (dr["PRODUCT_ID"].Equals(dr2["PRODUCT_ID"]))
                                {
                                    flag = true;
                                    ProdMrpQtyEntity entity = new ProdMrpQtyEntity(dr2);                                    
                                    entity.CurrentStorageQty = CURRENT_QTY;
                                    entity.PlanPurchaseQty = PURCHASE_TOTAL_QTY;
                                    entity.AvailableQty = CURRENT_QTY + PURCHASE_TOTAL_QTY - PLAN_TOTAL_QTY;
                                    DataAccess.Update(entity, broker);
                                    break;
                                }
                            }
                            if (!flag)
                            {
                                ProdMrpQtyEntity entity = new ProdMrpQtyEntity();
                                entity.ProductId = dr["PRODUCT_ID"].ToString();
                                entity.CurrentStorageQty = CURRENT_QTY;
                                entity.PlanPurchaseQty = PURCHASE_TOTAL_QTY;
                                entity.AvailableQty = CURRENT_QTY + PURCHASE_TOTAL_QTY - PLAN_TOTAL_QTY;
                                DataAccess.Insert(entity, broker);
                            }
                        }
                    }
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }
        #endregion
    }


}

